<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-data-layer-color-settings-panel" [formGroup]="colorSettingsFormGroup">
  <div class="tb-data-layer-color-settings-title" translate>widgets.maps.data-layer.color-settings</div>
  <div class="flex flex-row">
    <tb-toggle-select formControlName="type" class="flex-1">
      <tb-toggle-option [value]="DataLayerColorType.constant">
        {{ 'widgets.maps.data-layer.color-type-constant' | translate }}
      </tb-toggle-option>
      <tb-toggle-option [value]="DataLayerColorType.range">
        {{ 'widgets.maps.data-layer.color-type-range' | translate }}
      </tb-toggle-option>
      <tb-toggle-option [value]="DataLayerColorType.function">
        {{ 'widgets.maps.data-layer.color-type-function' | translate }}
      </tb-toggle-option>
    </tb-toggle-select>
  </div>
  <div class="tb-form-row space-between">
    <div translate>widgets.maps.data-layer.color</div>
    <tb-color-input asBoxInput
                    formControlName="color">
    </tb-color-input>
  </div>
  <div class="tb-data-layer-color-settings-panel-body" [class.!hidden]="colorSettingsFormGroup.get('type').value !== DataLayerColorType.constant">
  </div>
  <div class="tb-data-layer-color-settings-panel-body" [class.!hidden]="colorSettingsFormGroup.get('type').value !== DataLayerColorType.range">
    <tb-data-key-input style="padding-top: 6px;"
      inlineField="false"
      appearance="outline"
      subscriptSizing="fixed"
      label="{{ 'widgets.maps.data-layer.color-range-source-key' | translate }}"
      required
      requiredText="{{ 'widgets.maps.data-layer.color-range-source-key-required' }}"
      [datasourceType]="dsType"
      [entityAliasId]="dsEntityAliasId"
      [deviceId]="dsDeviceId"
      [aliasController]="context.aliasController"
      [widgetType]="widgetType.latest"
      [dataKeyType]="context.functionsOnly ? DataKeyType.function : null"
      [dataKeyTypes]="[DataKeyType.attribute, DataKeyType.timeseries]"
      [callbacks]="context.callbacks"
      [generateKey]="context.generateDataKey"
      (keyEdit)="editRangeKey()"
      formControlName="rangeKey">
    </tb-data-key-input>
    <div class="tb-form-panel stroked">
      <div translate>widgets.maps.data-layer.color-range</div>
      <tb-color-range-list class="tb-color-ranges-panel"
                           simpleRange
                           formControlName="range"
                           [popover]="popover">
      </tb-color-range-list>
    </div>
  </div>
  <div class="tb-data-layer-color-settings-panel-body" [class.!hidden]="colorSettingsFormGroup.get('type').value !== DataLayerColorType.function">
    <ng-container *ngTemplateOutlet="function"></ng-container>
  </div>
  <div class="tb-data-layer-color-settings-panel-buttons">
    <span class="flex-1"></span>
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button
            color="primary"
            type="button"
            (click)="applyColorSettings()"
            [disabled]="colorSettingsFormGroup.invalid || !colorSettingsFormGroup.dirty">
      {{ 'action.apply' | translate }}
    </button>
  </div>
</div>

<ng-template #function>
  <div class="tb-form-panel no-padding no-border" [formGroup]="colorSettingsFormGroup">
    <tb-js-func formControlName="colorFunction"
                withModules
                [functionArgs]="['data', 'dsData']"
                [globalVariables]="functionScopeVariables"
                functionTitle="{{ 'widgets.maps.data-layer.color-function' | translate }}"
                [helpId]="helpId"
                [helpPopupStyle]="{width: '900px'}">
    </tb-js-func>
  </div>
</ng-template>
